//
//  CmtSecDetailTableViewCell.m
//  Products_B
//
//  Created by lanou on 16/5/30.
//  Copyright © 2016年 朋学友. All rights reserved.
//

#import "CmtSecDetailTableViewCell.h"
#import "CmtSecAttenModel.h"
#import "ImageUpLabel.h"
#import "CmtSecCellCollectionViewCell.h"
#import "CmtSecCellSCollectionViewCell.h"
#import "CmtSecAttenLike.h"
#import "CmtSecAttenComment.h"
#import "CmtSecAttenRelates.h"
#import <UIImageView+WebCache.h>

#import "CmtSecMoreLikeViewController.h"
#import "CmtShenQuViewController.h"
#import "CmtProductDetailViewController.h"//作品详情


@interface CmtSecDetailTableViewCell ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)NSMutableArray *likeArr;
@property(nonatomic,strong)NSMutableArray *relatesArr;
@property(nonatomic,strong)NSMutableArray *commentArr;

@property(nonatomic,strong)UICollectionView *SeCollectionView;
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)NSString *user_id;

@property(nonatomic,strong)NSString *_id;

@end

@implementation CmtSecDetailTableViewCell


//创建第二个Cell
-(void)setCommentCellWithModel:(CmtSecAttenComment *)model withTapBlock:(TapBlock)topBlock{

    self.tapBlock = topBlock;
    
    UIView *view = [[UIView alloc]initWithFrame:self.contentView.bounds];
    
    view.backgroundColor = [UIColor whiteColor];

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, 150, 25)];
    label.text = [NSString stringWithFormat:@"%@条点评",model.total];
    label.font = [UIFont systemFontOfSize:15];
    [view addSubview:label];
    
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
    button.frame = CGRectMake(30, 50, kWidth - 60, 30);
    button.backgroundColor = [UIColor orangeColor];
    button.tintColor = [UIColor whiteColor];
    button.layer.cornerRadius = 15;

    [button setTitle:@"我也说两句" forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(CommentButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [view addSubview:button];
    
    [self.contentView addSubview:view];
   
}

-(void)CommentButtonAction:(UIButton *)button{


}

//第一个Cell
-(void)setCellWithModel:(CmtSecAttenModel *)model withTapBlock:(TapBlock)topBlock{


    self.tapBlock = topBlock;
    self.user_id = model.user_id;
    self._id = model._id;
    self.likeArr = model.likeArray;
    self.relatesArr = model.relatesArry;
    
//    上面点赞的label
    ImageUpLabel *ILView = [[ImageUpLabel alloc]initWithFrame:CGRectMake(10, 10, 200, 30) image:[UIImage imageNamed:@"cmtPraise.png"] textLabel:nil tapAction:topBlock];
    
    if (model.agree_count != 0) {
        
        [ILView setTextlabelText:[NSString stringWithFormat:@"%@位厨友觉得很赞",model.agree_count]];
    }
    
    [self.contentView addSubview:ILView];
    
//    点赞的CollectionView
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
   self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(20, 45, kWidth - 40, 30) collectionViewLayout:flowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_collectionView];
    
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
    button.frame = CGRectMake(kWidth - 70, 0, 30, 30);
    [button setTitleColor:[UIColor orangeColor] forState:(UIControlStateNormal)];
    [button setImage:[UIImage imageNamed:@"CmtRight.png"] forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(rightButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [_collectionView addSubview:button];
    
    [_collectionView registerClass:[CmtSecCellCollectionViewCell class] forCellWithReuseIdentifier:@"CmtSecCellItem"];
    
    if (model.topic_name != nil) {
//      中间更多达人作品的label
        
        //    大RelatesLabel
        UILabel *relateLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, kWidth, 25)];
        relateLabel.userInteractionEnabled = YES;
        //    左边的label
        UILabel *lfRelateLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 30, 25)];
        lfRelateLabel.font = [UIFont systemFontOfSize:15];
        lfRelateLabel.text = @"更多";
        
//       中间的button
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
        button.frame = CGRectMake(30, 0, 10, 25);
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        button.tintColor = [UIColor orangeColor];
        [button addTarget:self action:@selector(centerButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [button setTitle:model.topic_name forState:(UIControlStateNormal)];
        
//        动态返回button宽度
        NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:15] forKey:NSFontAttributeName];
        CGRect bounds = [model.topic_name boundingRectWithSize:CGSizeMake(100000,25) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
        CGRect frame = button.frame;
        frame.size = CGSizeMake(bounds.size.width,25);
        button.frame = frame;
        
        [button addTarget:self action:@selector(CmtSecCellButAction:) forControlEvents:(UIControlEventTouchUpInside)];
//        右边的label
        UILabel *RgRelatesLabel = [[UILabel alloc]initWithFrame:CGRectMake(30 + bounds.size.width, 0, 80, 25)];
        RgRelatesLabel.text = @"相关作品";
        RgRelatesLabel.font = [UIFont systemFontOfSize:15];
        
        [relateLabel addSubview:lfRelateLabel];
        [relateLabel addSubview:button];
        [relateLabel addSubview:RgRelatesLabel];
        [self.contentView addSubview:relateLabel];
    }
    
    
//   下面更多相关作品的CollectionView
    UICollectionViewFlowLayout *SecFlowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.SeCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 110, kWidth, 50) collectionViewLayout:SecFlowLayout];
    _SeCollectionView.delegate = self;
    _SeCollectionView.dataSource = self;
    _SeCollectionView.backgroundColor = [UIColor whiteColor];
    
    SecFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    [self.contentView addSubview:_SeCollectionView];
    
    [_SeCollectionView registerClass:[CmtSecCellSCollectionViewCell class] forCellWithReuseIdentifier:@"CmtSecCellSItem"];

}

//第二个Cell的评论按钮
-(void)CmtSecCellButAction:(UIButton *)button{

    NSLog(@"判断是否登录,再发表评论");
    
}

-(void)centerButtonAction:(UIButton *)button{

}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (collectionView == _SeCollectionView) {
        
        return _relatesArr.count;
    }

    return 6;
}

//返回Item
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
//    点赞人数的item
    if (collectionView == _collectionView) {
        
        
        UIImageView *coverImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
        coverImage.layer.cornerRadius = 15;
        coverImage.layer.masksToBounds = YES;
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CmtSecCellItem" forIndexPath:indexPath];
        [cell addSubview:coverImage];
        if (indexPath.row != 0) {
            CmtSecAttenLike *model = self.likeArr[indexPath.row];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [coverImage sd_setImageWithURL:[NSURL URLWithString:model.head_img]];
            });

            
        }
        
        return cell;
    }
    else{
//   相关作品的item
        UIImageView *coverImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CmtSecCellSItem" forIndexPath:indexPath];
        [cell addSubview:coverImage];
        cell.backgroundColor = [UIColor grayColor];
        CmtSecAttenRelates *model = self.relatesArr[indexPath.row];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [coverImage sd_setImageWithURL:[NSURL URLWithString:model.image]];
        });
        
        return cell;
    }
}

#pragma mark - 获取当前view所在的视图控制器
- (UIViewController *)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}


//更多赞的列表
-(void)rightButtonAction:(UIButton *)button{
    
    
    CmtSecMoreLikeViewController *CmtMLVc = [[CmtSecMoreLikeViewController alloc]init];
    CmtMLVc.post_id = __id;
    if ([__id isEqual: @""]) {
        CmtMLVc.post_id = [NSString stringWithFormat:@"0"];
    }
    
    [[self viewController].navigationController pushViewController:CmtMLVc animated:YES];
    [self viewController].navigationController.navigationBar.hidden = YES;;
    
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    if (collectionView == _collectionView) {//点赞
        
        CmtSecAttenLike *model = self.likeArr[indexPath.row];
        CmtShenQuViewController *CmtSQVc = [[CmtShenQuViewController alloc]init];
        CmtSQVc.user_id = model.user_id;
        [[self viewController].navigationController pushViewController:CmtSQVc animated:YES];
    }
    else{//相关作品
        CmtSecAttenRelates *model = self.relatesArr[indexPath.row];
        CmtProductDetailViewController *CmtSDTableVc = [[CmtProductDetailViewController alloc]init];
        CmtSDTableVc.post_id = model._id;
        [[self viewController].navigationController pushViewController:CmtSDTableVc animated:YES];

    }

}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

    return UIEdgeInsetsMake(0, 0, 0, 0);
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    if (collectionView == _SeCollectionView) {
        
        return CGSizeMake(50, 50);
    }

    
    return CGSizeMake(30, 30);
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 15;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
